﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Code4WP7SDK.Controls
{   
    //TODO 手势的支持
    public class CoverFlow : ItemsControl
    {
        #region Dependency Properties
        [Category("CoverFlow")]
        public object SelectedItem
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(object), typeof(CoverFlow), new PropertyMetadata(null));

        [Category("CoverFlow")]
        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CoverFlow), new PropertyMetadata(SelectedIndexChanged));

        public static void SelectedIndexChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            CoverFlow coverFlow = (CoverFlow)obj;
            if (coverFlow != null && coverFlow.Panel.FocusItemIndex != coverFlow.SelectedIndex)
            {
                coverFlow.Panel.FocusItemIndex = (int)e.NewValue;
                coverFlow.SelectedItem = coverFlow.Items[coverFlow.SelectedIndex];
            }
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            if (Panel != null && Panel.FocusItemIndex != SelectedIndex)
            {
                Panel.FocusItemIndex = SelectedIndex;
                SelectedItem = Items[SelectedIndex];
            }
            return base.GetContainerForItemOverride();
        }

        public CoverFlowPanel Panel
        {
            get
            {
                if (this.GetTemplateChild("ItemsPresenter") != null)
                {
                    return (CoverFlowPanel)VisualTreeHelper.GetChild(GetTemplateChild("ItemsPresenter"), 0);
                }
                return null;
            }
        }
        #endregion

        #region Constructor
        public CoverFlow()
        {
            this.DefaultStyleKey = typeof(CoverFlow);
        }
        #endregion
        


    }
}
